package com.test.utils;

import java.io.InputStream;
import java.util.Properties;

public class ConfigManager {
    private static ConfigManager instance;
    private Properties properties;

    private ConfigManager() {
        loadProperties();
    }

    public static ConfigManager getInstance() {
        if (instance == null) {
            instance = new ConfigManager();
        }
        return instance;
    }

    private void loadProperties() {
        properties = new Properties();
        try (InputStream input = getClass().getClassLoader().getResourceAsStream("config/test.properties")){
            if (input == null) {
                throw new RuntimeException("Unable to find test.properties file");
            }
            properties.load(input);
        } catch (Exception e) {
            //
        }
    }

    public String getProperty(String key) {
        String value = System.getProperty(key);

        if (value == null) {
            String envKey = key.toUpperCase().replace(".", "_");
            value = System.getenv(envKey);
        }

        if (value == null) {
            value = properties.getProperty(key);
        }

        return value;
    }

    public String getProperty(String key, String defaultValue) {
        String value = getProperty(key);
        return value != null ? value : defaultValue;
    }

    public int getIntProperty(String key, int defaultValue) {
        String value = properties.getProperty(key);
        if (value != null) {
            try {
                return Integer.parseInt(value);
            } catch (NumberFormatException e) {

            }
        }
        return defaultValue;
    }

    public boolean getBooleanProperty(String key, boolean defaultValue) {
        String value = properties.getProperty(key);
        if (value != null) {
            return Boolean.parseBoolean(value);
        }
        return defaultValue;
    }

    // Convenience methods for commonly used properties
    public String getSaraBaseUrl() {
        return getProperty("sara.base.url");
    }

    public String getSaraWorkflowUrl() {
        return getProperty("sara.workflow.url");
    }

    public String getSaraWorkflowListUrl() {
        return getProperty("sara.workflow.list.url");
    }

    public String getCookieName() {
        return getProperty("sara.cookie.name");
    }

    public String getCookieValue() {
        return getProperty("sara.cookie.value");
    }

    public boolean isBrowserHeadless() {
        return getBooleanProperty("headless", true);
    }

    public String getCookieDomain() {
        return getProperty("sara.cookie.domain");
    }

    public String getCookiePath() {
        return getProperty("sara.cookie.path");
    }

    public boolean isIgnoreSSLErrors() {
        return getBooleanProperty("ignore.ssl.errors", false);
    }

    public boolean isBypassCSP() {
        return getBooleanProperty("bypass.csp", false);
    }

    public int getViewportWidth() {
        return getIntProperty("viewport.width", 1280);
    }

    public int getViewportHeight() {
        return getIntProperty("viewport.height", 720);
    }

    public int getNavigationTimeout() {
        return getIntProperty("timeout.navigation", 60000);
    }

    public int getDefaultTimeout() {
        return getIntProperty("timeout.default", 30000);
    }
}
